package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProductionType;
import com.wing.productionPlan.model.request.ProductionTypeForm;
import com.wing.productionPlan.service.ProductionScheduleService;
import com.wing.productionPlan.service.ProductionTypeService;
import com.wing.qualityTesting.model.response.NodeDataVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.wechat.WechatCheckinService;
import com.wing.wechat.WechatDepartmentService;
import com.wing.wechat.WechatUserService;
import com.wing.wechat.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产排班管理
 *
 * @author xiayang
 */
@Validated
@Api(value = "生产排班管理", tags = "#/productionSchedule/productionSchedule")
@RestController("productionScheduleController")
@RequestMapping("productionSchedule/admin/productionSchedule")
@RequiredArgsConstructor
public class ProductionScheduleController {

    @Autowired
    ProductionScheduleService productionScheduleService;

    @ApiOperation(value = "企业微信-部门列表", notes = "搜索")
    @GetMapping("/department/list")
    public JsonResult departmentList(@RequestParam(name = "departmentId", required = false) Integer departmentId) {
        ResponseDepartmentList responseDepartmentList = productionScheduleService.departmentList(departmentId);
        if (responseDepartmentList.getErrcode() == 0) {
            return JsonResult.success(responseDepartmentList.getDepartment());
        }
        return JsonResult.fail("调用失败");
    }

    @ApiOperation(value = "企业微信-部门树形列表", notes = "搜索")
    @GetMapping("/department/getTreeData")
    public JsonResult getDepartmentTreeData() {
        ResponseDepartmentList responseDepartmentList = productionScheduleService.departmentList(null);
        if (responseDepartmentList.getErrcode() == 0) {
            Map<Long, List<NodeDataVO>> map = new HashMap<>();
            for (ResponseDepartmentVO departmentVO : responseDepartmentList.getDepartment()) {
                if (map.containsKey(departmentVO.getParentid().longValue())) {
                    NodeDataVO nodeDataVO = new NodeDataVO();
                    nodeDataVO.setValue(departmentVO.getId().toString());
                    nodeDataVO.setName(departmentVO.getName());
                    nodeDataVO.setPid(departmentVO.getParentid().toString());
                    map.get(departmentVO.getParentid().longValue()).add(nodeDataVO);
                } else {
                    List<NodeDataVO> tmpList = new ArrayList<>();
                    NodeDataVO nodeDataVO = new NodeDataVO();
                    nodeDataVO.setValue(departmentVO.getId().toString());
                    nodeDataVO.setName(departmentVO.getName());
                    nodeDataVO.setPid(departmentVO.getParentid().toString());
                    tmpList.add(nodeDataVO);
                    map.put(departmentVO.getParentid().longValue(), tmpList);
                }
            }
            List<NodeDataVO> rootList = map.get(0L);
            if (rootList == null) {
                return JsonResult.success();
            }
            constructTree(rootList, map);
            return JsonResult.success(rootList);
        }
        return JsonResult.fail("调用失败");
    }


    @ApiOperation(value = "企业微信-更新部门信息", notes = "更新")
    @PostMapping("/department/update")
    public JsonResult departmentUpdate(RequestUpdateDepartment updateDepartment) {
        ResponseUpdateDepartment responseUpdateDepartment = productionScheduleService.updateDepartment(updateDepartment);
        if (responseUpdateDepartment.getErrcode() == 0) {
            return JsonResult.success("更新成功");
        }
        return JsonResult.fail("更新失败");
    }

    @ApiOperation(value = "企业微信-删除部门信息", notes = "删除")
    @GetMapping("/department/delete")
    public JsonResult departmentDelete(@RequestParam(name = "departmentId", required = false) Integer departmentId) {
        ResponseDeleteDepartment responseDeleteDepartment = productionScheduleService.deleteDepartment(departmentId);
        if (responseDeleteDepartment.getErrcode() == 0) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "企业微信-部门成员列表", notes = "搜索")
    @GetMapping("/user/list")
    public JsonResult userList(@RequestParam(name = "departmentId", required = false) Integer departmentId) {
        ResponseDepartmentUserList departmentUserList = productionScheduleService.departmentUserList(departmentId);
        if (departmentUserList.getErrcode() == 0) {
            return JsonResult.success(departmentUserList.getUserlist());
        }
        return JsonResult.fail("调用失败");
    }

    @ApiOperation(value = "企业微信-更新部门成员信息", notes = "更新部门成员")
    @PostMapping("/user/update")
    public JsonResult userUpdate(RequestUpdateUser updateUser) {
        ResponseUpdateUser responseUpdateUser = productionScheduleService.updateUser(updateUser);
        if (responseUpdateUser.getErrcode() == 0) {
            return JsonResult.success("更新成功");
        }
        return JsonResult.fail("更新失败");
    }

    @ApiOperation(value = "企业微信-删除部门成员信息", notes = "删除部门成员")
    @GetMapping("/user/delete")
    public JsonResult userDelete(@RequestParam(name = "userId", required = false) String userId) {
        ResponseDeleteUser deleteUser = productionScheduleService.deleteUser(userId);
        if (deleteUser.getErrcode() == 0) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "企业微信-成员排班列表", notes = "搜索")
    @GetMapping("/schedule/list")
    public JsonResult scheduleList(@RequestParam(name = "queryTime", required = false) String queryTime,
                                   @RequestParam(name = "userid", required = false) String userid) {
        ResponseCheckinSchedule responseCheckinSchedule = productionScheduleService.scheduleList(queryTime, userid);
        if (responseCheckinSchedule.getErrcode() == 0) {
            return JsonResult.success(responseCheckinSchedule.getSchedule_list());
        }
        return JsonResult.fail("调用失败");
    }

    /**
     * 树形数据 转换
     *
     * @param parentList
     * @param dataMap
     */
    private void constructTree(List<NodeDataVO> parentList, Map<Long, List<NodeDataVO>> dataMap) {
        for (NodeDataVO node : parentList) {
            Long nodeId = Long.valueOf(node.getValue());
            List<NodeDataVO> childList = dataMap.get(nodeId);
            if (childList != null) {
                node.setChildren(childList);
                constructTree(childList, dataMap);
            }
        }
    }

}
